package com.erp.myAdapter

import android.annotation.SuppressLint
import androidx.appcompat.app.AlertDialog
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.erp.bean.AddJiaFangBean
import com.erp.R
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.lv_xiangxi_jiafang_item.view.*


/**
 * Created by Administrator on 2018/6/26/026.
 * 作者：刘洋
 */
class JiaFangXiangXIAdapter(val context : Context, val mlist: MutableList<AddJiaFangBean>):BaseAdapter() {

    var minflater: LayoutInflater = LayoutInflater.from(context)
    private var mTouchItemPosition = -1
    override fun getItem(position: Int): Any {
        return mlist[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return mlist.size
    }
    @SuppressLint("ViewHolder")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var v=minflater.inflate(R.layout.lv_xiangxi_jiafang_item,null)
        val bean = mlist[position]
        Myutil.settv(bean.code,v.TvCode)
        Myutil.settv(bean.name,v.TvName)
        Myutil.settv(bean.o_onum,v.TvDanHao)
        v.LlItem.setOnLongClickListener {
            val dialog = AlertDialog.Builder(context)
            dialog.setTitle("提示")
            dialog.setMessage("确定要删除     " + mlist[position].code+ "     这条单吗?")
            dialog.setPositiveButton("确定") { dialog, which ->
                mlist.removeAt(position)
                dialog.dismiss()
                notifyDataSetChanged()
            }
            dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
            dialog.show()
            false
        }
        v.EtAmount.setText(bean.amount)
        v.EtAmount.tag = position
        v.EtAmount.setOnTouchListener { v, event ->
            Log.d("TAAG","aaaaaaaaaaaaaaaaaa")
            mTouchItemPosition = v.tag as Int
            false
        }
        if (mTouchItemPosition == position) {
            v.EtAmount.isFocusable = true
            v.EtAmount.requestFocus()
            v.EtAmount.setSelection( v.EtAmount.text.toString().length)
        } else {
            v.EtAmount.clearFocus()
        }
        mTouchItemPosition=-1
        v.EtAmount.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if(s!=null) {
                    mlist[position].amount = s.toString()
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

        })
        return v
    }
}